package com.cloudpivot.project.crmprojectnew.util;

import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * crm常用工具类
 */
@Slf4j
public class CrmNormalUtil {
    public static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }

    public static String getUpperGuid() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        return uuid.toString().toUpperCase();

    }

    public static String getLowerGuid() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        return uuid.toString().toLowerCase();

    }

    public static String getNowTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return dateFormat.format(new Date());
    }

    /**
     * 获取格林威志当前时间
     *
     * @return
     */
    public static String getGMTDateTime() {
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, -8);
        return sdf.format(calendar.getTime());
    }

    /**
     * 转换日期为格林威治时间
     *
     * @param _s
     * @return
     */
    public static String convertGMTDateTime(String _s) {
        if (_s == null) {
            return null;
        }
        SimpleDateFormat s_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = s_sdf.parse(_s);

        } catch (ParseException pe) {
            nowDate = new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, -8);
        return sdf.format(calendar.getTime());
    }

    /**
     * 转换格林时间到中国区时间
     *
     * @param gmt
     * @return
     */
    public static String convertGMTDateTimeToNormal(String gmt) {
        if (gmt == null) {
            return null;
        }
        SimpleDateFormat s_sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Date nowDate = null;
        try {
            nowDate = s_sdf.parse(gmt);

        } catch (ParseException pe) {
            nowDate = new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, +8);
        return sdf.format(calendar.getTime());
    }

    public static String getCrmDomainname(String _s) {
        if ("".equals(_s) || _s == null) {
            return null;
        } else {
            if (_s.contains("@")) {
                String[] user = _s.split("@");
                String returnStr = String.format("BIOSAN\\%s", user[0]);
                return returnStr;
            } else {
                return null;
            }
        }
    }

    /**
     * 转义crmurl
     *
     * @param _s
     * @return
     */
    public static String encodeCrmUrl(String _s) {
        String url = "";
        url = _s.replace(" ", "%20").replace("'", "%27").replace("\\", "%5C");
        return url;
    }

    /**
     * 分页方法
     *
     * @param _pageSize  每页记录数
     * @param _pageIndex 页码 必须大于0
     * @param inList     待分页List
     * @param <T>
     * @return
     */
    public static <T> List<T> paging(int _pageIndex, int _pageSize, List<T> inList) {

        int pageSize = _pageSize;
        int count = inList.size();
        int pageNum = _pageIndex ;

        List<T> outList = new ArrayList<>();
        int startIndex = pageSize * pageNum + 1;
        int endIndex = startIndex + pageSize;
        for (int i = startIndex; i < endIndex; i++) {
            if (i <= count) {
                outList.add(inList.get(i - 1));
            }
        }
        log.info("分页执行结束");
        return outList;


    }

    /**
     * crm调用 分页字符串
     */
    public static String getFetchPage(int pageIndex, int pageSize, String entity) {
        try {
            String fetchStr = "fetchXml=<fetch page=\"%d\" count=\"%d\"><entity name=\"%s\"></entity></fetch>&$count=true";
            String paramStr = String.format(fetchStr, pageIndex+1, pageSize, entity);
            return paramStr;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 编码url
     * @param str
     * @return
     */
    public static String urlEcode(String str){
        try{
            String returnStr = new URI(null, str, null).toURL().toString();
            return returnStr;
        }catch(Exception ex){
            ex.printStackTrace();
            return null;
        }

    }
}
